﻿namespace AC.ExtendedRenderer.Toolkit.Drawing
{
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class KryptonColorChooser2 : KryptonForm
    {
        internal KryptonButton btnCancel;
        internal KryptonButton btnOK;
        private ChangeStyle changeType = ChangeStyle.None;
        private Container components;
        internal HScrollBar hsbBlue;
        internal HScrollBar hsbBrightness;
        internal HScrollBar hsbGreen;
        internal HScrollBar hsbHue;
        internal HScrollBar hsbRed;
        internal HScrollBar hsbSaturation;
        private ColorHandler.HSV HSV;
        internal Label Label1;
        internal Label Label2;
        internal Label Label3;
        internal Label Label5;
        internal Label Label6;
        internal Label Label7;
        internal Label lblBlue;
        internal Label lblBrightness;
        internal Label lblGreen;
        internal Label lblHue;
        internal Label lblRed;
        internal Label lblSaturation;
        private ColorWheel myColorWheel;
        internal Panel pnlBrightness;
        internal Panel pnlColor;
        internal Panel pnlSelectedColor;
        private ColorHandler.RGB RGB;
        private Point selectedPoint;
        private TextBox txtWebColor;

        public KryptonColorChooser2()
        {
            this.InitializeComponent();
        }

        private void ColorChooser2_Load(object sender, EventArgs e)
        {
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.DoubleBuffer, true);
            this.pnlSelectedColor.Visible = false;
            this.pnlBrightness.Visible = false;
            this.pnlColor.Visible = false;
            Rectangle selectedColorRectangle = new Rectangle(this.pnlSelectedColor.Location, this.pnlSelectedColor.Size);
            Rectangle brightnessRectangle = new Rectangle(this.pnlBrightness.Location, this.pnlBrightness.Size);
            Rectangle colorRectangle = new Rectangle(this.pnlColor.Location, this.pnlColor.Size);
            this.myColorWheel = new ColorWheel(colorRectangle, brightnessRectangle, selectedColorRectangle);
            this.myColorWheel.ColorChanged = (ColorWheel.ColorChangedEventHandler) Delegate.Combine(this.myColorWheel.ColorChanged, new ColorWheel.ColorChangedEventHandler(this.myColorWheel_ColorChanged));
            this.SetRGB(this.RGB);
            this.SetHSV(this.HSV);
        }

        private void ColorChooser2_Paint(object sender, PaintEventArgs e)
        {
            switch (this.changeType)
            {
                case ChangeStyle.MouseMove:
                case ChangeStyle.None:
                    this.myColorWheel.Draw(e.Graphics, this.selectedPoint);
                    return;

                case ChangeStyle.RGB:
                    this.myColorWheel.Draw(e.Graphics, this.RGB);
                    return;

                case ChangeStyle.HSV:
                    this.myColorWheel.Draw(e.Graphics, this.HSV);
                    return;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void frmMain_MouseUp(object sender, MouseEventArgs e)
        {
            this.myColorWheel.SetMouseUp();
            this.changeType = ChangeStyle.None;
        }

        private void HandleHSVScroll(object sender, ScrollEventArgs e)
        {
            this.changeType = ChangeStyle.HSV;
            this.HSV = new ColorHandler.HSV(this.hsbHue.Value, this.hsbSaturation.Value, this.hsbBrightness.Value);
            this.SetRGB(ColorHandler.HSVtoRGB(this.HSV));
            this.SetHSVLabels(this.HSV);
            base.Invalidate();
        }

        private void HandleMouse(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.changeType = ChangeStyle.MouseMove;
                this.selectedPoint = new Point(e.X, e.Y);
                base.Invalidate();
            }
        }

        private void HandleRGBScroll(object sender, ScrollEventArgs e)
        {
            this.changeType = ChangeStyle.RGB;
            this.RGB = new ColorHandler.RGB(this.hsbRed.Value, this.hsbGreen.Value, this.hsbBlue.Value);
            this.SetHSV(ColorHandler.RGBtoHSV(this.RGB));
            this.SetRGBLabels(this.RGB);
            base.Invalidate();
        }

        private void InitializeComponent()
        {
            this.lblBlue = new Label();
            this.lblGreen = new Label();
            this.lblRed = new Label();
            this.lblBrightness = new Label();
            this.lblSaturation = new Label();
            this.lblHue = new Label();
            this.hsbBlue = new HScrollBar();
            this.hsbGreen = new HScrollBar();
            this.hsbRed = new HScrollBar();
            this.hsbBrightness = new HScrollBar();
            this.hsbSaturation = new HScrollBar();
            this.hsbHue = new HScrollBar();
            this.btnCancel = new KryptonButton();
            this.btnOK = new KryptonButton();
            this.Label3 = new Label();
            this.Label7 = new Label();
            this.pnlColor = new Panel();
            this.Label6 = new Label();
            this.Label1 = new Label();
            this.Label5 = new Label();
            this.pnlSelectedColor = new Panel();
            this.pnlBrightness = new Panel();
            this.Label2 = new Label();
            this.txtWebColor = new TextBox();
            base.SuspendLayout();
            this.lblBlue.Location = new Point(0x138, 360);
            this.lblBlue.Name = "lblBlue";
            this.lblBlue.Size = new Size(40, 0x17);
            this.lblBlue.TabIndex = 0x36;
            this.lblBlue.TextAlign = ContentAlignment.MiddleLeft;
            this.lblGreen.Location = new Point(0x138, 0x150);
            this.lblGreen.Name = "lblGreen";
            this.lblGreen.Size = new Size(40, 0x17);
            this.lblGreen.TabIndex = 0x35;
            this.lblGreen.TextAlign = ContentAlignment.MiddleLeft;
            this.lblRed.Location = new Point(0x138, 0x138);
            this.lblRed.Name = "lblRed";
            this.lblRed.Size = new Size(40, 0x17);
            this.lblRed.TabIndex = 0x34;
            this.lblRed.TextAlign = ContentAlignment.MiddleLeft;
            this.lblBrightness.Location = new Point(0x138, 280);
            this.lblBrightness.Name = "lblBrightness";
            this.lblBrightness.Size = new Size(40, 0x17);
            this.lblBrightness.TabIndex = 0x33;
            this.lblBrightness.TextAlign = ContentAlignment.MiddleLeft;
            this.lblSaturation.Location = new Point(0x138, 0x100);
            this.lblSaturation.Name = "lblSaturation";
            this.lblSaturation.Size = new Size(40, 0x17);
            this.lblSaturation.TabIndex = 50;
            this.lblSaturation.TextAlign = ContentAlignment.MiddleLeft;
            this.lblHue.Location = new Point(0x138, 0xe8);
            this.lblHue.Name = "lblHue";
            this.lblHue.Size = new Size(40, 0x17);
            this.lblHue.TabIndex = 0x31;
            this.lblHue.TextAlign = ContentAlignment.MiddleLeft;
            this.hsbBlue.LargeChange = 1;
            this.hsbBlue.Location = new Point(80, 360);
            this.hsbBlue.Maximum = 0xff;
            this.hsbBlue.Name = "hsbBlue";
            this.hsbBlue.Size = new Size(0xe0, 0x12);
            this.hsbBlue.TabIndex = 0x30;
            this.hsbBlue.Scroll += new ScrollEventHandler(this.HandleRGBScroll);
            this.hsbGreen.LargeChange = 1;
            this.hsbGreen.Location = new Point(80, 0x150);
            this.hsbGreen.Maximum = 0xff;
            this.hsbGreen.Name = "hsbGreen";
            this.hsbGreen.Size = new Size(0xe0, 0x12);
            this.hsbGreen.TabIndex = 0x2f;
            this.hsbGreen.Scroll += new ScrollEventHandler(this.HandleRGBScroll);
            this.hsbRed.LargeChange = 1;
            this.hsbRed.Location = new Point(80, 0x138);
            this.hsbRed.Maximum = 0xff;
            this.hsbRed.Name = "hsbRed";
            this.hsbRed.Size = new Size(0xe0, 0x12);
            this.hsbRed.TabIndex = 0x2e;
            this.hsbRed.Scroll += new ScrollEventHandler(this.HandleRGBScroll);
            this.hsbBrightness.LargeChange = 1;
            this.hsbBrightness.Location = new Point(80, 280);
            this.hsbBrightness.Maximum = 0xff;
            this.hsbBrightness.Name = "hsbBrightness";
            this.hsbBrightness.Size = new Size(0xe0, 0x12);
            this.hsbBrightness.TabIndex = 0x2d;
            this.hsbBrightness.Scroll += new ScrollEventHandler(this.HandleHSVScroll);
            this.hsbSaturation.LargeChange = 1;
            this.hsbSaturation.Location = new Point(80, 0x100);
            this.hsbSaturation.Maximum = 0xff;
            this.hsbSaturation.Name = "hsbSaturation";
            this.hsbSaturation.Size = new Size(0xe0, 0x12);
            this.hsbSaturation.TabIndex = 0x2c;
            this.hsbSaturation.Scroll += new ScrollEventHandler(this.HandleHSVScroll);
            this.hsbHue.LargeChange = 1;
            this.hsbHue.Location = new Point(80, 0xe8);
            this.hsbHue.Maximum = 0xff;
            this.hsbHue.Name = "hsbHue";
            this.hsbHue.Size = new Size(0xe0, 0x12);
            this.hsbHue.TabIndex = 0x2b;
            this.hsbHue.Scroll += new ScrollEventHandler(this.HandleHSVScroll);
            this.btnCancel.DialogResult = DialogResult.Cancel;
            this.btnCancel.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.btnCancel.Location = new Point(0x128, 40);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x40, 0x18);
            this.btnCancel.TabIndex = 0x2a;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Values.ExtraText = "";
            this.btnCancel.Values.Image = null;
            this.btnCancel.Values.ImageStates.ImageCheckedNormal = null;
            this.btnCancel.Values.ImageStates.ImageCheckedPressed = null;
            this.btnCancel.Values.ImageStates.ImageCheckedTracking = null;
            this.btnCancel.Values.Text = "Cancel";
            this.btnOK.DialogResult = DialogResult.OK;
            this.btnOK.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.btnOK.Location = new Point(0x128, 8);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new Size(0x40, 0x18);
            this.btnOK.TabIndex = 0x29;
            this.btnOK.Text = "OK";
            this.btnOK.Values.ExtraText = "";
            this.btnOK.Values.Image = null;
            this.btnOK.Values.ImageStates.ImageCheckedNormal = null;
            this.btnOK.Values.ImageStates.ImageCheckedPressed = null;
            this.btnOK.Values.ImageStates.ImageCheckedTracking = null;
            this.btnOK.Values.Text = "OK";
            this.Label3.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label3.Location = new Point(8, 360);
            this.Label3.Name = "Label3";
            this.Label3.Size = new Size(0x48, 0x12);
            this.Label3.TabIndex = 0x22;
            this.Label3.Text = "Blue";
            this.Label3.TextAlign = ContentAlignment.MiddleLeft;
            this.Label7.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label7.Location = new Point(8, 280);
            this.Label7.Name = "Label7";
            this.Label7.Size = new Size(0x48, 0x12);
            this.Label7.TabIndex = 0x25;
            this.Label7.Text = "Brightness";
            this.Label7.TextAlign = ContentAlignment.MiddleLeft;
            this.pnlColor.Location = new Point(8, 8);
            this.pnlColor.Name = "pnlColor";
            this.pnlColor.Size = new Size(0xe0, 0xd8);
            this.pnlColor.TabIndex = 0x26;
            this.pnlColor.Visible = false;
            this.Label6.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label6.Location = new Point(8, 0x100);
            this.Label6.Name = "Label6";
            this.Label6.Size = new Size(0x48, 0x12);
            this.Label6.TabIndex = 0x24;
            this.Label6.Text = "Saturation";
            this.Label6.TextAlign = ContentAlignment.MiddleLeft;
            this.Label1.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label1.Location = new Point(8, 0x138);
            this.Label1.Name = "Label1";
            this.Label1.Size = new Size(0x48, 0x12);
            this.Label1.TabIndex = 0x20;
            this.Label1.Text = "Red";
            this.Label1.TextAlign = ContentAlignment.MiddleLeft;
            this.Label5.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label5.Location = new Point(8, 0xe8);
            this.Label5.Name = "Label5";
            this.Label5.Size = new Size(0x48, 0x12);
            this.Label5.TabIndex = 0x23;
            this.Label5.Text = "Hue";
            this.Label5.TextAlign = ContentAlignment.MiddleLeft;
            this.pnlSelectedColor.Location = new Point(0x128, 0x48);
            this.pnlSelectedColor.Name = "pnlSelectedColor";
            this.pnlSelectedColor.Size = new Size(0x40, 0x18);
            this.pnlSelectedColor.TabIndex = 40;
            this.pnlSelectedColor.Visible = false;
            this.pnlBrightness.Location = new Point(240, 8);
            this.pnlBrightness.Name = "pnlBrightness";
            this.pnlBrightness.Size = new Size(0x18, 0xd8);
            this.pnlBrightness.TabIndex = 0x27;
            this.pnlBrightness.Visible = false;
            this.Label2.Font = new Font("Microsoft Sans Serif", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.Label2.Location = new Point(8, 0x150);
            this.Label2.Name = "Label2";
            this.Label2.Size = new Size(0x48, 0x12);
            this.Label2.TabIndex = 0x21;
            this.Label2.Text = "Green";
            this.Label2.TextAlign = ContentAlignment.MiddleLeft;
            this.txtWebColor.Location = new Point(0x128, 0x66);
            this.txtWebColor.Name = "txtWebColor";
            this.txtWebColor.Size = new Size(0x40, 20);
            this.txtWebColor.TabIndex = 0x39;
            this.AutoScaleBaseSize = new Size(5, 13);
            this.BackColor = System.Drawing.Color.White;
            base.ClientSize = new Size(0x170, 0x185);
            base.Controls.Add(this.txtWebColor);
            base.Controls.Add(this.lblBlue);
            base.Controls.Add(this.lblGreen);
            base.Controls.Add(this.lblRed);
            base.Controls.Add(this.lblBrightness);
            base.Controls.Add(this.lblSaturation);
            base.Controls.Add(this.lblHue);
            base.Controls.Add(this.hsbBlue);
            base.Controls.Add(this.hsbGreen);
            base.Controls.Add(this.hsbRed);
            base.Controls.Add(this.hsbBrightness);
            base.Controls.Add(this.hsbSaturation);
            base.Controls.Add(this.hsbHue);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOK);
            base.Controls.Add(this.Label3);
            base.Controls.Add(this.Label7);
            base.Controls.Add(this.pnlColor);
            base.Controls.Add(this.Label6);
            base.Controls.Add(this.Label1);
            base.Controls.Add(this.Label5);
            base.Controls.Add(this.pnlSelectedColor);
            base.Controls.Add(this.pnlBrightness);
            base.Controls.Add(this.Label2);
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.Name = "KryptonColorChooser2";
            this.Text = "Select Color";
            base.Paint += new PaintEventHandler(this.ColorChooser2_Paint);
            base.MouseUp += new MouseEventHandler(this.frmMain_MouseUp);
            base.MouseMove += new MouseEventHandler(this.HandleMouse);
            base.MouseDown += new MouseEventHandler(this.HandleMouse);
            base.Load += new EventHandler(this.ColorChooser2_Load);
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void myColorWheel_ColorChanged(object sender, ColorChangedEventArgs e)
        {
            this.SetRGB(e.RGB);
            this.SetHSV(e.HSV);
        }

        private void RefreshText(Label lbl, int value)
        {
            lbl.Text = value.ToString();
        }

        private void RefreshValue(HScrollBar hsb, int value)
        {
            hsb.Value = value;
        }

        private void SetHSV(ColorHandler.HSV HSV)
        {
            this.RefreshValue(this.hsbHue, HSV.Hue);
            this.RefreshValue(this.hsbSaturation, HSV.Saturation);
            this.RefreshValue(this.hsbBrightness, HSV.value);
            this.SetHSVLabels(HSV);
        }

        private void SetHSVLabels(ColorHandler.HSV HSV)
        {
            this.RefreshText(this.lblHue, HSV.Hue);
            this.RefreshText(this.lblSaturation, HSV.Saturation);
            this.RefreshText(this.lblBrightness, HSV.value);
        }

        private void SetRGB(ColorHandler.RGB RGB)
        {
            this.RefreshValue(this.hsbRed, RGB.Red);
            this.RefreshValue(this.hsbBlue, RGB.Blue);
            this.RefreshValue(this.hsbGreen, RGB.Green);
            this.SetRGBLabels(RGB);
        }

        private void SetRGBLabels(ColorHandler.RGB RGB)
        {
            this.RefreshText(this.lblRed, RGB.Red);
            this.RefreshText(this.lblBlue, RGB.Blue);
            this.RefreshText(this.lblGreen, RGB.Green);
            this.txtWebColor.Text = ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(RGB.Red, RGB.Green, RGB.Blue)).ToString();
        }

        public System.Drawing.Color Color
        {
            get
            {
                return this.myColorWheel.Color;
            }
            set
            {
                this.changeType = ChangeStyle.RGB;
                this.RGB = new ColorHandler.RGB(value.R, value.G, value.B);
                this.HSV = ColorHandler.RGBtoHSV(this.RGB);
            }
        }

        private enum ChangeStyle
        {
            MouseMove,
            RGB,
            HSV,
            None
        }
    }
}

